#include <string>

#include "hilog/log.h"
#include "securec.h"

#include "ai_service_napi_utils.h"
#include "ai_service_napi_error.h"
#include "ai_service_errors.h"
#include "ai_service_log.h"

using OHOS::HiviewDFX::HiLog;
using OHOS::HiviewDFX::HiLogLabel;

namespace OHOS {
namespace AIS {
namespace {
constexpr HiLogLabel LABEL = {LOG_CORE, AIS_LOG_DOMAIN, "AIServiceNapi"};
constexpr int SIZE = 1048576;
}

bool GetNapiInt32(const napi_env &env, const int32_t value, napi_value &result)
{
    napi_status ret = napi_create_int32(env, value, &result);
    if (ret != napi_ok)
    {
        AIS_HILOGE("GetNapiInt32 failed");
        return false;
    }
    return true;
}

bool GetPropertyString(const napi_env &env, const napi_value &value, const std::string &type, std::string &result)
{
    napi_value item = nullptr;
    bool exist = false;
    char buffer[SIZE];
    size_t length = 0;

    napi_status status = napi_has_named_property(env, value, type.c_str(), &exist);
    if (status != napi_ok || !exist) {
        AIS_HILOGW("can not find %{public}s", type.c_str());
        return false;
    }

    if (napi_get_named_property(env, value, type.c_str(), &item) != napi_ok) {
        AIS_HILOGW("fail to get property: %{public}s", type.c_str());
        return false;
    }

    if (napi_get_value_string_utf8(env, item, buffer, SIZE, &length) != napi_ok) {
        AIS_HILOGW("fail to get property value: %{public}s", type.c_str());
        return false;
    }
    result = std::string(buffer);
    return true;
}

bool GetPropertyInt32(const napi_env &env, const napi_value &value, const std::string &type, int32_t &result)
{
  napi_value item = nullptr;
  bool exist = false;
  napi_status status = napi_has_named_property(env, value, type.c_str(), &exist);

  if (status != napi_ok || !exist) {
    AIS_HILOGW("can not find  %{public}s ", type.c_str());
    return false;
  }

  if (napi_get_named_property(env, value, type.c_str(), &item) != napi_ok) {
    AIS_HILOGW("fail to get property: %{public}s ", type.c_str());
    return false;
  }

  if (napi_get_value_int32(env, item, &result) != napi_ok) {
    AIS_HILOGW("fail to get property value: %{public}s ", type.c_str());
    return false;
  }
  return true;
}

void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
                        napi_value &result)
{
    napi_value value = nullptr;
    napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &value);
    napi_set_named_property(env, result, fieldStr.c_str(), value);
}

void Uint8ArrayToJsValue(const napi_env &env, std::vector<uint8_t> &uint8Buffer, size_t bufferSize, napi_value &result)
{
    uint8_t *nativeArraybuffer = nullptr;
    napi_create_arraybuffer(env, bufferSize, reinterpret_cast<void **>(&nativeArraybuffer), &result);

    errno_t ret = memcpy_s(nativeArraybuffer, bufferSize, uint8Buffer.data(), bufferSize);
    if (ret != EOK)
    {
        AIS_HILOGE("memcpy_s failed\n");
        return;
    }
}

} // namespace AIS
} // namespace OHOS